package com.example.dynamicdatasource.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Auther: feima
 * @Date: 2020-10-12 17:20
 * @Description:
 */
@Configuration
@EnableAsync
public class ThreadPool {

    /**
     * 库存对账同时拉取线程池 只在凌晨5点使用 可被公用
     * @return
     */
    @Bean("saveExecutor")
    public Executor saveExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(2);
        taskExecutor.setMaxPoolSize(2);
        taskExecutor.setKeepAliveSeconds(6000);
        taskExecutor.setThreadNamePrefix("saveExecutor--");
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(6000);
        //配置拒绝策略 抛异常
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        return taskExecutor;
    }


}
